class LibraryController < ApplicationController
  attr_reader :collection

  layout 'library', :except => ['rss', 'atom']
  session :off, :only => [:rss, :atom]

  def index
    if (params[:page] != nil)
      @quotations = Quotation.find(:all, :page => { :size => 10, :current => params[:page] }, :order => 'created_on DESC')
    else
      @quotations = Quotation.find(:all, :page => { :size => 10 }, :order => 'created_on DESC')
    end
    @total_quotes = Quotation.count
  end
  
  # GETs should be safe (see http://www.w3.org/2001/tag/doc/whenToUseGet.html)
  verify :method => :post, :only => [ :destroy, :create, :update ],
         :redirect_to => { :action => :list }

  def library
    # @quotation_pages, @quotations = paginate :quotations, :conditions => "is_active = 'Y'", :per_page => 10, :order => 'created_on DESC'
    @quotations = Quotation.find(:all, :page => { :size => 10 }, :order => 'created_on DESC')
  end
  
  def libraryhtml
    library
  end
  
  def home
    @total_quotes = Quotation.count
    library
  end
  
  def homehtml
    home
  end

  def showajax
    show
  end
    
  def show
    @quotation = Quotation.find(params[:id])
  end

  def new
    @quotation = Quotation.new
  end

  def create
    @quotation = Quotation.new(params[:quotation])
    @quotation.created_on = Time.now
    if @quotation.save
      flash[:notice] = 'Quotation was successfully created.'
      library
      render :action => 'libraryhtml'
    else
      render :action => 'newhtml'
    end
  end

  def edit
    @quotation = Quotation.find(params[:id])
  end

  def update
    @quotation = Quotation.find(params[:id])
    if @quotation.update_attributes(params[:quotation])
      flash[:notice] = 'Quotation was successfully updated.'
      redirect_to :action => 'show', :id => @quotation
    else
      render :action => 'edit'
    end
  end

  def destroy
    Quotation.find(params[:id]).destroy
    redirect_to :action => 'list'
  end
  
  def submitsearch
    if request.post?
      search_string = '%' + params[:search_for] + '%'
      @quotations = Quotation.find(:all, :conditions => ["(content like ? OR author like ?) AND is_active = 'Y'", search_string, search_string], :order => 'created_on DESC')
    end
  end
  
  def rss
    render_rss_feed_for(Quotation.find(:all, :conditions => "is_active = 'Y'", :order => 'created_on DESC', :limit => 10),
                    { :feed => {:title => 'Latest from Quoteopedia.com'},
                      :item => {:title => :fullquote, :pub_date => :created_on} })
  end
  
  def atom
    render_atom_feed_for(Quotation.find(:all, :conditions => "is_active = 'Y'", :order => 'created_on DESC', :limit => 10),
                    { :feed => {:title => 'Latest from Quoteopedia.com'},
                      :item => {:title => :fullquote, :pub_date => :created_on} })
  end

  def quotation_url(post)
    return url_for(:controller => 'library', :action => 'show', :id => post)
  end
  
  def quotations_url
    return url_for(:controller => 'library', :action => 'index')
  end
  
end
